﻿App.Donebill = function() {
    return {

        currentFormValues: {},

        getStore: function() {
            var store = new Ext.data.Store({
                autoLoad:true,
				baseParams : {start:0, limit:25},
				proxy: new Ext.data.HttpProxy({
					url : 'cost/donebill_list'
				}),
                remoteSort: false,
				reader: new Ext.data.JsonReader({
					totalProperty : 'totalCount',
					root : 'rows',
					idProperty : 'bill_id',
					fields: [{
						name: 'bill_id',
						type: 'string'
					},{
						name: 'bill_sum_fee',
						type: 'string'
					},
					{
						name: 'bill_type',
						allowBlank: false
					},
					{
						name: 'name',
						allowBlank: false
					},
					{
						name: 'local_name',
						allowBlank: false
					},
					{
						name:'op_time',
						type: 'date',
						dateFormat: 'time'
					}]
				})
            });
            return store;},
       createGrid: function(id) {

            var panel = Ext.getCmp(id);

            panel.body.dom.innerHTML = "";

            var sm = new Ext.grid.CheckboxSelectionModel();
            this.grid = new Ext.grid.GridPanel({
            	anchor:'top 50%',
                loadMask: true,
                stripeRows: true,
                tbar: new Ext.PagingToolbar({ 
                	pageSize: 20 ,//分页必须加的参数.最好和store里面的limit保持一致
                	displayInfo: true,
                    store: this.store,
                    items:[
                    {
	                    text: '接款',
	                    scope: this,
	                    handler: this.edit
	                },
	                '-', new Ext.ux.form.SearchField({
                    store: this.store,
                    width: 110
                })]}),
                title:"接款管理",//打印必须设置tile
                plugins: [Ext.ux.plugins.Print],//打印按钮
                loadMask: true,
                viewConfig : {autoFill:false,forceFit:true},
                autoHeight:true,
                store: this.store,
                sm: sm,
                columns: [sm,new Ext.grid.RowNumberer(), 
                          {
        			hidden:true,
        			header:"打款ID",
        			dataIndex:'bill_id'
                  },{
            header: "打款人",
            width: 60,
            sortable: true,
            dataIndex: 'name',
            renderer: this.highLight
        },{
            header: "打款分点",
            width: 60,
            sortable: true,
            dataIndex: 'local_name',
            renderer: this.highLight
        },
        {
        	header: "打款方式",
            width: 150,
            sortable: true,
            dataIndex: 'bill_type'
        },{
            header: "总费用",
            width: 60,
            sortable: true,
            dataIndex: 'bill_sum_fee',
            renderer: this.highLight
        },{
            header: "时间",
            width: 60,
            sortable: true,
            dataIndex: 'op_time',
            renderer: Ext.util.Format.dateRenderer('Y-m-d H:i:s')
        }]
            });
            panel.add(this.grid);

        },
        
        
        submit: function() {
            if (this.form.getForm().isValid()) {
                this.form.getForm().submit({
                    waitTitle: '保存数据',
                    waitMsg: '正在保存……',
                    scope: this,
                    method: 'post',
                    params: '',
                    success: function(form, action) {
                       var info = action.result.info;
                        if (action.result.success) {
                            this.store.reload();
                            if (action.result.method == "Create") {
                                this.form.getForm().reset();
                            } else {
                                this.dlg.hide();
                            }
                        }
                        Ext.Msg.alert('信息', info);
                    },
                    failure: function(form, action) {
                        var info = action.result.info;
	    				Ext.Msg.alert('提示', info);
                    }
                });
            }
        },

        getForm: function() {
            var frm = new Ext.form.FormPanel({
            	url:'cost/donebillok',
                labelAlign: 'right',
                frame: true,
                waitMsgTarget:true,
                defaults:{allowBlank :false},
                defaultType: 'textfield',
                items: [
                        {
                        	xtype:"hidden",
                        	name:'bill_id'
                        },
                        {
                  			name: 'local_name',
                  			readOnly:true,
                			fieldLabel: '分点'
                        },
                        {
                  			name: 'name',
                  			readOnly:true,
                			fieldLabel: '打款人'
                        },
                        {
                  			name: 'bill_sum_fee',
                  			readOnly:true,
                			fieldLabel: '本次下账总费用'
                        }], 
            buttons: [{
                    text: '确定',
                    scope: this,
                    handler: function() {
                        this.submit();
                    }
                }]
            }); //FormPanel
            return frm;
        },
        getDialog: function() {
            var dlg = new Ext.Window({
                width: 550,
                height: 300,
                title: '',
                plain: true,
                closable: true,
                resizable: false,
                frame: true,
                layout: 'fit',
                closeAction: 'hide',
                border: false,
                modal: true,
                items: [this.form],
                listeners: {
                    scope: this,
                    render: function(fp) {
                        this.form.getForm().waitMsgTarget = fp.getEl();
                    },
                    show: function() {
                        var f = this.form.getForm();
                 //       f.findField("roleList").store.reload();
                        f.setValues(this.currentFormValues);
                        f.clearInvalid();
//                        if (this.currentFormValues.id) {
//                            Ext.get("pwdMsg").dom.innerHTML = "<span style='color:red;line-height:30px;'>*如果不修改密码请不要输入密码和确认密码</span>";
//                            f.findField("password").allowBlank = true;
//                            f.findField("confirmPassword").allowBlank = true;
//                        } else {
//                            f.findField("password").allowBlank = false;
//                            f.findField("confirmPassword").allowBlank = false;
//                            Ext.get("pwdMsg").dom.innerHTML = "<span style='color:red;'></span>";
//                        }
                    }
                }
            });
            return dlg;
        },
        highLight: function(value) {
        	var key = App.Donebill.grid.store.baseParams.query;
        	var v = value ? value.toString() : '';
        	if (Ext.isString(v) & Ext.isString(key)) {
        		if (key.length > 0) {
        			var re = new RegExp(key, "g");
        			return v.replace(re, "<span style='color:red;background-color:yellow;'>" + key + "</span>");
        		} else {
        			return v;
        		}
        	} else {
        		return v;
        	}
        },

        edit: function() {
            if (this.grid.getSelectionModel().hasSelection()) {
                this.dlg.setTitle("接账管理");
                var selectdata = this.grid.getSelectionModel().getSelected().data;;
             //   if (this.grid.getSelectionModel().hasSelection()) {
             //       var recs = this.grid.getSelectionModel().getSelections();
                Ext.apply(this.currentFormValues, {
                	bill_id:selectdata.bill_id,
                	name:selectdata.name,
                	local_name:selectdata.local_name,
            		bill_sum_fee:selectdata.bill_sum_fee
            		
               });
                this.dlg.show();
           // }
            }else {
                Ext.Msg.alert('信息', '请选择要回收的欠款。');
            }
        },


        render: function(id) {
            if (!this.store) {
                this.store = this.getStore();
            };
            if (!this.form) {
               this.form = this.getForm();
            };
            if (!this.dlg) {
                this.dlg = this.getDialog();
            };
            this.createGrid(id);

        }
    };
} ();
